package com.tbit.uqbike.service.rpcservice.impl;

import com.tbit.uqbike.object.pojo.Account;
import com.tbit.uqbike.object.pojo.AccountSmsModel;
import com.tbit.uqbike.object.pojo.BrandRemoteConfig;
import com.tbit.uqbike.object.pojo.SmsAuthConfig;
import com.tbit.uqbike.service.business.AccountSmsModelService;
import com.tbit.uqbike.service.rpcservice.SmsModelService;
import com.tbit.uqbike.webmanager.dao.core.AccountDao;
import com.tbit.uqbike.webmanager.dao.core.SmsAuthConfigDao;
import com.tbit.uqbike.webmanager.sms.ApiStoreMsgUtils;
import com.tbit.uqbike.webmanager.sms.ClzySmsAuthUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

/**
 * @Author: chen
 * @Description:
 * @Date: 创建于 11:00 2022/7/20
 */
@Service
public class SmsModelServiceImpl implements SmsModelService {

    @Autowired
    private AccountSmsModelService accountSmsModelService;

    @Autowired
    private SmsAuthConfigDao smsAuthConfigDao;
    @Autowired
    private AccountDao accountDao;

    /**
     * 获取发送短信文本内容
     *
     * @param content 模板
     * @param param   附加参数
     * @return
     */
    public static String getContent(String content, Map<String, String> param) {
        /**构造发送短信内容*/
        content = content.replace("#", "");
        for (Map.Entry<String, String> entry : param.entrySet()) {
            content = content.replace(entry.getKey(), entry.getValue());
        }
        return content;
    }

    /**
     * 获取模板短信附加参数
     *
     * @param param
     * @return
     */
    public static String getModelContent(Map<String, String> param) {
        StringBuffer stringBuffer = new StringBuffer();
        stringBuffer.append("{");
        for (Map.Entry<String, String> entry : param.entrySet()) {
            stringBuffer.append("\"" + entry.getKey() + "\"");
            stringBuffer.append(":\"" + entry.getValue() + "\"");
            stringBuffer.append(",");
            // content=content.replace(entry.getKey(),entry.getValue());
        }
        stringBuffer.delete(stringBuffer.length() - 1, stringBuffer.length());
        stringBuffer.append("}");
        return stringBuffer.toString();
    }

    public static void main(String[] args) {
        Map<String, String> param = new HashMap<>();
        param.put("accountId", "深圳区域");
        param.put("userCode", "3231");
        param.put("alarm", "测试告警");
        //String content ="车辆预警：#accountId# 车辆#userCode#产生#alarm#异常，请及时处理";
        //content=content.replace("#","");
        System.out.println(getModelContent(param));
        // System.out.println(content);
    }

    @Override
    public void sendSmsByModel(Integer accountId, String phone, Integer type,
                               Map<String, String> param) {
        /***/
        AccountSmsModel accountSmsModel = new AccountSmsModel();
        accountSmsModel.setAccountId(accountId);
        accountSmsModel.setSmsType(type);
        AccountSmsModel accountSmsModelDB = accountSmsModelService.queryById(accountSmsModel);
        if (accountSmsModelDB == null) {
            return;
        }
        Map<String, Integer> mapSMS = new HashMap<>();
        mapSMS.put("accountId", accountId);
        mapSMS.put("configType", 4);
        Account brandArea = accountDao.getByAccountId(accountId);
        BrandRemoteConfig brandRemoteConfig = smsAuthConfigDao.getByConfigType(mapSMS);
        if (brandRemoteConfig != null) {
            String[] strings = brandRemoteConfig.getParamKV().split("&");
            String accounts = strings[0].substring(strings[0].indexOf("=") + 1);
            String password = strings[1].substring(strings[1].indexOf("=") + 1);
            /**构造短信内容*/
            String content = getContent(accountSmsModelDB.getSmsContent(), param);
            ClzySmsAuthUtil.sendSms(accounts, password, "【" + brandArea.getName() + "】" + content, phone);
        } else {
            SmsAuthConfig smsAuthConfig = smsAuthConfigDao.getByAccountId(accountId);
            if (smsAuthConfig != null) {
                if (smsAuthConfig.getFrom() == 0) {
                    /**构造短信内容*/
                    String content = getContent(accountSmsModelDB.getSmsContent(), param);
                    ApiStoreMsgUtils.sendContent(phone, smsAuthConfig.getSmsCodeKey(), "【" + brandArea.getName() + "】" + content);
                }
            }

        }

    }

}
